home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 6 / develop 6 code / TCP / NewsWatcher / NewsWatcher 2.0d15 source / source / popup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-17  |  6.9 KB  |  253 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     popup.c
  4.  
  5.     This module contains miscellaneous popup menu utility routines.
  6.     
  7.     Portions copyright © 1990, Apple Computer.
  8.     Portions copyright © 1993, Northwestern University.
  9.  
  10. ----------------------------------------------------------------------------*/
  11.  
  12. #include <Packages.h>
  13.  
  14. #include "dlgutil.h"
  15. #include "popup.h"
  16.  
  17.  
  18.  
  19. /* This is the private data for a popup menu control using CDEF 63.
  20.    It's documented in IM6:Compatibility, and in NIM:Toolbox Essentials. */
  21.    
  22. typedef struct {
  23.     MenuHandle mHandle;        /* Handle to menu */
  24.     short mID;                /* Menu ID */
  25.     char mPrivate;            /* Private data of varying length */
  26. } popupPrivateData;
  27.  
  28.  
  29.  
  30. /*----------------------------------------------------------------------------
  31.     GetPopupPString
  32.     
  33.     Gets the text of an item in a Popup Menu control, as a Pascal format
  34.     string.
  35.     
  36.     Entry:    ctl = handle to popup menu control.
  37.             item = item number to get, or kCurrentPopupItem for 
  38.                 the currently selected item.
  39.                 
  40.     Exit:    str = the item text, as a Pascal format string.
  41. ----------------------------------------------------------------------------*/
  42.  
  43. void GetPopupPString (ControlHandle    ctl, short item, Str255    str)
  44. {
  45.     popupPrivateData **data;
  46.     
  47.     data = (popupPrivateData**)(**ctl).contrlData;
  48.     if (item == kCurrentPopupItem) item = GetCtlValue(ctl);
  49.     GetItem((**data).mHandle, item, str);
  50. }
  51.  
  52.  
  53.  
  54. /*----------------------------------------------------------------------------
  55.     GetPopupCString
  56.     
  57.     Gets the text of an item in a Popup Menu control, as a C format
  58.     string.
  59.     
  60.     Entry:    ctl = handle to popup menu control.
  61.             item = item number to get, or kCurrentPopupItem for 
  62.                 the currently selected item.
  63.                 
  64.     Exit:    str = the item text, as a C format string.
  65. ----------------------------------------------------------------------------*/
  66.  
  67. void GetPopupCString (ControlHandle ctl, short item, char *str)
  68. {
  69.     GetPopupPString(ctl, item, (StringPtr)str);
  70.     p2cstr((StringPtr)str);
  71. }
  72.  
  73.  
  74.  
  75. /*----------------------------------------------------------------------------
  76.     SetPopupItemStyle
  77.     
  78.     Sets the text style for one of the items in a popup menu control.
  79.     
  80.     Entry:    ctl = handle to popup menu control.
  81.             item = item number to set, or kCurrentPopupItem for 
  82.                 the currently selected item.
  83.             style = the new text style.
  84. ----------------------------------------------------------------------------*/
  85.  
  86. void SetPopupItemStyle (ControlHandle ctl, short item, short style)
  87. {
  88.     popupPrivateData **data;
  89.     
  90.     data = (popupPrivateData**)(**ctl).contrlData;
  91.     if (item == kCurrentPopupItem) item = GetCtlValue(ctl);
  92.     SetItemStyle((**data).mHandle, item, style);
  93. }
  94.  
  95.  
  96.  
  97. /*----------------------------------------------------------------------------
  98.     AddPopupItem
  99.     
  100.     Adds an item to a popup menu control.
  101.     
  102.     Entry:    ctl = handle to popup menu control.
  103.             after = item number after which the new item should be
  104.                 inserted, or 0 to insert the new item and the beginning
  105.                 of the menu.
  106.             str = text or the new item, in Pascal format.
  107. ----------------------------------------------------------------------------*/
  108.  
  109. void AddPopupItem (ControlHandle ctl, short after, Str255 str)
  110. {
  111.     popupPrivateData **data;
  112.     short curValue;
  113.  
  114.     data = (popupPrivateData**)(**ctl).contrlData;
  115.     InsMenuItem((**data).mHandle, str, after);
  116.     curValue = GetCtlValue(ctl);
  117.     if (after < curValue) SetCtlValue(ctl, curValue+1);
  118. }
  119.  
  120.  
  121.  
  122. /*----------------------------------------------------------------------------
  123.     DelPopupItem
  124.     
  125.     Deletes an item from a popup menu control.
  126.     
  127.     Entry:    ctl = handle to popup menu control.
  128.             item = item number to delete, or kCurrentPopupItem to delete 
  129.                 the currently selected item.
  130. ----------------------------------------------------------------------------*/
  131.  
  132. void DelPopupItem (ControlHandle ctl, short item)
  133. {
  134.     popupPrivateData **data;
  135.     short curValue;
  136.  
  137.     data = (popupPrivateData**)(**ctl).contrlData;
  138.     curValue = GetCtlValue(ctl);
  139.     if (item == kCurrentPopupItem) item = curValue;
  140.     DelMenuItem((**data).mHandle, item);
  141.     if (item < curValue) SetCtlValue(ctl, curValue-1);
  142. }
  143.  
  144.  
  145. /*----------------------------------------------------------------------------
  146.     SetPopupValue
  147.     
  148.     Sets the value of a popup menu control to the item matching a string.
  149.     
  150.     Entry:    ctl = handle to popup menu control.
  151.             str = Pascal format string.
  152.             isNumber = true if string and menu items are numbers.
  153.  
  154.     Exit:    function result = new control value, or 0 if no matching
  155.                 item.
  156. ----------------------------------------------------------------------------*/
  157.  
  158. short SetPopupValue (ControlHandle ctl, Str255 str, Boolean    isNumber)
  159. {
  160.     popupPrivateData **data;
  161.     long checkVal, itemVal;
  162.     MenuHandle menu;
  163.     short numItems, item;
  164.     Str255 tempStr;
  165.  
  166.     data = (popupPrivateData**)(**ctl).contrlData;
  167.     if (isNumber) StringToNum(str, &checkVal);
  168.     menu = (**data).mHandle;
  169.  
  170.     numItems = CountMItems(menu);
  171.     for (item = 1; item <= numItems; item++) {
  172.         GetItem(menu, item, tempStr);
  173.         if (isNumber) {
  174.             StringToNum(tempStr, &itemVal);
  175.             if (checkVal == itemVal) {
  176.                 SetCtlValue(ctl, item);
  177.                 return item;
  178.             }
  179.         } else if (EqualString(str, tempStr, false, true)) {
  180.             SetCtlValue(ctl, item);
  181.             return item;
  182.         }
  183.     }
  184.     return 0;
  185. }
  186.  
  187. /*----------------------------------------------------------------------------
  188.     TrackPopup
  189.     
  190.     Tracks a click in a typein popup menu control.
  191.     
  192.     Entry:    ctl = handle to popup menu control.
  193.             where = location of mouse click, in local coords.
  194.             checkItem = text of the item that should appear checked
  195.                 (the contents of the "partner" typein textedit field),
  196.                 in Pascal format.
  197.             isNumber = true if menu items are numbers.
  198.             
  199.     Exit:    function result = new control value, or 0 if no change.
  200.  
  201.     If the text does not match a menu item, a new item is added
  202.     at the beginning, followed by a separator line. These two extra
  203.     items are deleted before the function returns. 
  204. ----------------------------------------------------------------------------*/
  205.  
  206. short TrackPopup (ControlHandle    ctl, Point where, Str255 checkItem, Boolean    isNumber)
  207. {
  208.     popupPrivateData **data;
  209.     MenuHandle menu;
  210.     long checkVal, itemVal;
  211.     short numItems, i, itemType, itemsAdded, part, newValue, oldValue;
  212.     Boolean    found;
  213.     Str255 tempStr;
  214.  
  215.     data = (popupPrivateData **) (**ctl).contrlData;
  216.     menu = (**data).mHandle;
  217.     itemsAdded = 0;
  218.     if (checkItem && *checkItem) {
  219.         oldValue = SetPopupValue(ctl, checkItem, isNumber);
  220.         if (oldValue == 0) {
  221.             if (isNumber) {
  222.                 StringToNum(checkItem, &checkVal);
  223.                 NumToString(checkVal, tempStr);
  224.                 InsMenuItem(menu, tempStr, 0);
  225.             } else {
  226.                 InsMenuItem(menu, checkItem, 0);
  227.             }
  228.             InsMenuItem(menu, "\p(-", 1);
  229.             oldValue = 1;
  230.             itemsAdded = 2;
  231.             SetCtlValue(ctl, oldValue);
  232.         }
  233.     } else {
  234.         oldValue = 0;
  235.     }
  236.  
  237.     part = TrackControl(ctl, where, (ProcPtr)-1);
  238.     newValue = GetCtlValue(ctl);
  239.  
  240.     if (part != 0 && oldValue != newValue) {
  241.         newValue = newValue - itemsAdded;
  242.     } else {
  243.         newValue = 0;
  244.     }
  245.     if (itemsAdded) {
  246.         for (i = 0; i < itemsAdded; i++) {
  247.             DelMenuItem(menu, 1);
  248.         }
  249.         SetCtlValue(ctl, newValue);
  250.     }
  251.     return newValue;
  252. }
  253.